import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { NzMessageService } from 'ng-zorro-antd';
import { retry } from 'rxjs/operators';

declare var form: any;
declare var base: any;

@Component({
  templateUrl: './map.administrative.html'
})
export class MapAdministrativeComponent implements OnInit {
  fs: any = null;
  tableName = '';
  fieldName = '';
  compFieldName = '';
  isEditVisible = false;
  constructor(
    private http: HttpClient,
    private msg: NzMessageService) {
    // 注册表单
    this.fs = new form.FormTreeService('adminlocal', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id'],
      pageSize: 1000,
      defaultObject: { leaf: false },
      queryFields: 'id,code,display,abbreviation,order,valid,info,pid,level,leaf'
    });
    // 加载表单数据
    this.fs.loadData();
  }

  closeEdit() {
    this.isEditVisible = false;
  }

  openEdit() {
    this.isEditVisible = true;
  }

  initConfig() {
    this.msg.info('开始初始化');
    const url: string = base.BaseService.getHostUrl() + '/adminlocal/init/' + this.tableName + '/' + this.fieldName + '/' + this.compFieldName;
    const obsData = this.http.post<boolean>(url, null, { withCredentials: true })
      .pipe(retry(1));

    obsData.subscribe(
      (data) => {
        if (data) {
          this.closeEdit();
          this.msg.info('加载中');
          this.fs.loadData();
          this.msg.info('加载成功');
        } else {
          this.msg.error('初始化失败，请确认锁定数据为空，以及能访问到政区数据!');
        }
      },
      (error) => {
        this.msg.error('初始化失败，请确认锁定数据为空，以及能访问到政区数据!');
      }
    );
  }

  ngOnInit(): void {
  }
}
